package com.yanqu.road.dao.impl.kuang.cross;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.kuang.battle.CrossKuangBattlePlaceData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossKuangBattlePlaceDataDaoImpl extends TempDataObjectDao<CrossKuangBattlePlaceData> {

    public static String getCrossKuangBattlePlaceDataSql = "select * from `t_c_kuang_battle_place_data` where `weekId`=? and `groupId`=? ";

    public Map<Integer, CrossKuangBattlePlaceData> getCrossKuangBattlePlaceDataMap(int weekId, int groupId) {
        Map<Integer, CrossKuangBattlePlaceData> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getCrossKuangBattlePlaceDataSql);
            int i = 1;
            call.setInt(i++, weekId);
            call.setInt(i++, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossKuangBattlePlaceData parse = parse(rs);
                map.put(parse.getPlaceId(), parse);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateCrossKuangBattlePlaceDataSql = "update `t_c_kuang_battle_place_data` set campId=?,seizeTime=?,lastSeizeRewardTime=?,updateTime=?,seizeInfoMap=?,lastSeizeCampId=?,lastNoticeSeizeTime=? where weekId=? and groupId=? and placeId=?";

    @Override
    protected boolean updateData(CrossKuangBattlePlaceData crossKuangBattlePlaceData) {
        crossKuangBattlePlaceData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.INTEGER, crossKuangBattlePlaceData.getCampId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossKuangBattlePlaceData.getSeizeTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossKuangBattlePlaceData.getLastSeizeRewardTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossKuangBattlePlaceData.getUpdateTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossKuangBattlePlaceData.getSeizeInfoMap())));
        para.put(i++, new DbParameter(Types.INTEGER, crossKuangBattlePlaceData.getLastSeizeCampId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossKuangBattlePlaceData.getLastNoticeSeizeTime()));
        para.put(i++, new DbParameter(Types.INTEGER, crossKuangBattlePlaceData.getWeekId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossKuangBattlePlaceData.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossKuangBattlePlaceData.getPlaceId()));
        if (getDb().executeNoneQuery(updateCrossKuangBattlePlaceDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossKuangBattlePlaceDataSql = "insert into `t_c_kuang_battle_place_data`(weekId,groupId,placeId,campId,seizeTime,lastSeizeRewardTime,updateTime,seizeInfoMap,lastSeizeCampId,lastNoticeSeizeTime) values (?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(CrossKuangBattlePlaceData crossKuangBattlePlaceData) {
        crossKuangBattlePlaceData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.INTEGER, crossKuangBattlePlaceData.getWeekId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossKuangBattlePlaceData.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossKuangBattlePlaceData.getPlaceId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossKuangBattlePlaceData.getCampId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossKuangBattlePlaceData.getSeizeTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossKuangBattlePlaceData.getLastSeizeRewardTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossKuangBattlePlaceData.getUpdateTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossKuangBattlePlaceData.getSeizeInfoMap())));
        para.put(i++, new DbParameter(Types.INTEGER, crossKuangBattlePlaceData.getLastSeizeCampId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossKuangBattlePlaceData.getLastNoticeSeizeTime()));
        if (getDb().executeNoneQuery(insertCrossKuangBattlePlaceDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossKuangBattlePlaceData parseData(ResultSet rs) throws SQLException {
        CrossKuangBattlePlaceData crossKuangBattlePlaceData = new CrossKuangBattlePlaceData();
        crossKuangBattlePlaceData.setWeekId(rs.getInt("weekId"));
        crossKuangBattlePlaceData.setGroupId(rs.getInt("groupId"));
        crossKuangBattlePlaceData.setPlaceId(rs.getInt("placeId"));
        crossKuangBattlePlaceData.setCampId(rs.getInt("campId"));
        crossKuangBattlePlaceData.setSeizeTime(rs.getLong("seizeTime"));
        crossKuangBattlePlaceData.setLastSeizeRewardTime(rs.getLong("lastSeizeRewardTime"));
        crossKuangBattlePlaceData.setUpdateTime(rs.getLong("updateTime"));
        String seizeInfoMap = rs.getString("seizeInfoMap");
        if (!StringUtils.isNullOrEmpty(seizeInfoMap)) {
            crossKuangBattlePlaceData.setSeizeInfoMap(JSON.parseObject(seizeInfoMap, new TypeReference<java.util.Map<java.lang.Integer, com.yanqu.road.entity.kuang.battle.CrossKuangBattlePlaceSeizeInfo>>() {
            }));
        }
        crossKuangBattlePlaceData.setLastSeizeCampId(rs.getInt("lastSeizeCampId"));
        crossKuangBattlePlaceData.setLastNoticeSeizeTime(rs.getLong("lastNoticeSeizeTime"));

        return crossKuangBattlePlaceData;
    }

}
